#!/bin/sh

# Slackware LibreOffice Dependency Check - swlodc
# A simple shell script to check if the dependency autodetection
# variables for the LibreOffice slackbuild are still sane.

# Copyright 2015-2020 Hunter Sezen California, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

PKGDIR=${PKGDIR:-/var/log/packages}

set -eu

exists () {
  r=0; cwd="$(pwd)"
  while [ $# -gt 0 ]; do
    v=1; arg="$1"; shift
    case "$arg" in
      ''|*/ )
        :
      ;;
      /* )
        if [ -f "$arg" ] && [ -x "$arg" ]; then
          printf %s\\n "$arg"
          v=0
        fi
      ;;
      ./* )
        if [ -f "$arg" ] && [ -x "$arg" ]; then
          pre="$(cd -- "${arg%%/*}/" && pwd)"
          printf %s\\n "${pre%/}/$arg"
          v=0
        fi
      ;;
      */* )
        if [ -f "$arg" ] && [ -x "$arg" ]; then
          printf %s\\n "$(cd -- "${arg%%/*}/.." && pwd)/$arg"
          v=0
        fi
      ;;
      * )
        if [ -n "${PATH+x}" ]; then
          p=":${PATH:-$cwd}"
          while [ "$p" != "${p#*:}" ] && [ -n "${p#*:}" ]; do
            p="${p#*:}"; x="${p%%:*}"; z="${x:-$cwd}"; d="${z%/}/$arg"
            if [ -f "$d" ] && [ -x "$d" ]; then
              case "$d" in
                /* ) : ;;
                ./* ) pre="$(cd -- "${d%/*}/" && pwd)"; d="${pre%/}/$d" ;;
                * ) d="$(cd -- "${d%/*}/" && pwd)/$arg" ;;
              esac
              printf %s\\n "$d"
              v=0
              break
            fi
          done
        fi
      ;;
    esac
  [ $v = 0 ] || r=1
  done
  return $r
}

check_type () {
  case "$1" in
    -b )
      exists "$PKG" >/dev/null 2>&1 && return 0
      TYPE="bin/$PKG"
      CMD="printf %s\\n \"warning: binary file for \$PRGNAM not found\""
    ;;
    -i )
      [ -d "/usr/include/$PKG" ] && return 0
      TYPE="include/$PKG"
      CMD="printf %s\\n \"warning: include directory for \$PRGNAM not found\""
    ;;
    -l )
      ldconfig -p | grep -q "$PKG" && return 0
      TYPE="$PKG"
      CMD="printf %s\\n \"warning: shared library for \$PRGNAM not found\""
    ;;
    -p )
      pkg-config --exists "$PKG" && return 0
      TYPE="$PKG.pc"
      CMD="grep pkgconfig/.*.pc \$PKGDIR/$PRGNAM-[0-9]*"
    ;;
  esac
  return 1
}

check_pkg () {
  PRGNAM="$2"
  case "$3" in
    '' ) PKG="$2" ;;
    * ) eval "PKG=$3" ;;
  esac
  if check_type "$1"; then
    printf %s\\n "$PRGNAM: ok"
  else
    printf %s\\n "$PRGNAM: not found" "warning: $TYPE is missing" >&2
    set -- "${PKGDIR}/${PRGNAM}*"
    if [ -r "$1" ]; then
      eval "$CMD" >&2
    else
      printf %s\\n "warning: $PRGNAM is not installed" >&2
    fi
  fi
}

# Check dependencies
check_pkg -p apr \$PRGNAM-1
check_pkg -p bluez ''
check_pkg -i boost ''
check_pkg -b bzip2 ''
check_pkg -p cairo ''
check_pkg -p clucene lib\$PRGNAM-core
check_pkg -p coinmp ''
check_pkg -p cppunit ''
check_pkg -p curl lib\$PRGNAM
check_pkg -p epoxy ''
check_pkg -p expat ''
check_pkg -p gc atomic_ops
check_pkg -i glm ''
check_pkg -p graphite2 ''
check_pkg -p gstreamer \$PRGNAM-1.0
check_pkg -p gtk+-3.0 ''
check_pkg -p harfbuzz ''
check_pkg -p hunspell ''
check_pkg -p icu4c icu-i18n
check_pkg -p lcms2 ''
check_pkg -p libabw \$PRGNAM-0.1
check_pkg -p libcdr \$PRGNAM-0.1
check_pkg -p libcmis \$PRGNAM-0.5
check_pkg -p libe-book \$PRGNAM-0.1
check_pkg -p libeot ''
check_pkg -p libepubgen \$PRGNAM-0.1
check_pkg -p libetonyek \$PRGNAM-0.1
check_pkg -p libexttextcat ''
check_pkg -p libfreehand \$PRGNAM-0.1
check_pkg -p libiodbc ''
check_pkg -p liblangtag ''
check_pkg -p libmspub \$PRGNAM-0.1
check_pkg -p libmwaw \$PRGNAM-0.3
check_pkg -p libnumbertext ''
check_pkg -p libodfgen \$PRGNAM-0.1
check_pkg -p liborcus \$PRGNAM-0.16
check_pkg -p libpagemaker \$PRGNAM-0.0
check_pkg -p libpng "\${PRGNAM}16"
check_pkg -p libqxp \$PRGNAM-0.0
check_pkg -p librevenge \$PRGNAM-0.0
check_pkg -p libstaroffice \$PRGNAM-0.0
check_pkg -l libtommath \$PRGNAM.so.1
check_pkg -p libvisio \$PRGNAM-0.1
check_pkg -p libwpd \$PRGNAM-0.10
check_pkg -p libwpg \$PRGNAM-0.3
check_pkg -p libwps \$PRGNAM-0.4
check_pkg -p libxml \$PRGNAM-2.0
check_pkg -p libzmf \$PRGNAM-0.0
check_pkg -b lpsolve lp_solve
check_pkg -b mariadb mysql
check_pkg -p mdds \$PRGNAM-1.5
check_pkg -p mozilla-nss ''
check_pkg -p mythes ''
check_pkg -p neon ''
check_pkg -p openjpeg libopenjp2
check_pkg -b openldap ldapurl
check_pkg -p openssl ''
check_pkg -p poppler ''
check_pkg -p postgresql libpq
check_pkg -p python3 ''
check_pkg -i qrcodegen ''
check_pkg -p redland ''
check_pkg -p sane \$PRGNAM-backends
check_pkg -p serf \$PRGNAM-1
check_pkg -p ucpp lib\$PRGNAM
check_pkg -p xmlsec "\${PRGNAM}1-nss"
check_pkg -p zlib ''
